package qa.tools.commom;


import qa.tools.commom.enums.ResponseCode;

import java.io.Serializable;

/**
 * @author yuanfei
 * @date 2018/6/15
 */
public class ApiResult<T> implements Serializable {

    private static final long serialVersionUID = -4892755760746431731L;

    private int code = ResponseCode.Success.getCode();

    private String message = ResponseCode.Success.getMsg();

    private T data;

    private ApiResult(int code, String message, T data) {
        this.code = code;
        this.message = message;
        this.data = data;
    }

    public ApiResult() {
    }

    public static <T> ApiResult succeedWith(T data) {
        return new ApiResult<T>(ResponseCode.Success.getCode(), ResponseCode.Success.getMsg(), data);
    }
    public static <T> ApiResult succeedWith(T data, String message) {
        return new ApiResult(ResponseCode.Success.getCode(), message, data);
    }

    public static <T> ApiResult success() {
        return new ApiResult(ResponseCode.Success.getCode(), ResponseCode.Success.getMsg(), null);
    }

    public static <T> ApiResult failedFor(int code, String message) {
        return new ApiResult(code, message, null);
    }

    public static <T> ApiResult failedFor(ResponseCode responseCode) {
        return new ApiResult(responseCode.getCode(), responseCode.getMsg(), null);
    }

    public static <T> ApiResult failedFor(int code, T data, String message) {
        return new ApiResult(code, message, data);
    }


    public int getCode() {
        return code;
    }
    public ApiResult<T> setCode(int code) {
        this.code = code;
        return this;
    }
    public String getMessage() {
        return message;
    }
    public ApiResult<T> setMessage(String message) {
        this.message = message;
        return this;
    }
    public T getData() {
        return data;
    }
    public ApiResult<T> setData(T data) {
        this.data = data;
        return this;
    }

    public boolean isSuccess() {
        return this.code == ResponseCode.Success.getCode() || this.code == ResponseCode.SUCCESS.getCode();
    }

    public long getTime() {
        return System.currentTimeMillis();
    }

    @Override
    public String toString() {
        return "ApiResult{" +
                "code=" + code +
                ", message='" + message + '\'' +
                ", data=" + data +
                '}';
    }

}
